package com.limf.demo.java8;

import java.util.function.Function;

public class FunctionTest {

	public static void main(String[] args) {
		test3();
	}
	static void test() {
		Function<String, Integer> toInteger = Integer::valueOf;
		Function<String, String> backToString = toInteger.andThen(String::valueOf);
		Function<Object, Integer> aString = toInteger.compose(String::valueOf);
		System.out.println(backToString.apply("123"));
		System.out.println(aString.apply("123"));
		
	}
	
	static void test2() {
		Function<Integer, Integer> test1 = i->i+1;
		Function<Integer, Integer> test2 = i->i*i;
		
		System.out.println(calculate(test1, 5));
		System.out.println(calculate(test2, 5));
		
	}
	public static Integer calculate(Function<Integer, Integer> test, Integer number) {
		return test.apply(number);
	}
	
	public static void test3() {
		Function<Integer, Integer> A = i->i+1;
		Function<Integer, Integer> B = i->i*i;
		System.out.println("F1:"+A.apply(5));
		System.out.println("F2:"+B.apply(5));
		
		System.out.println("---------------------------");
		System.out.println("F1:" + B.apply(A.apply(5)));
		System.out.println("F1:"+B.compose(A).apply(5));
		
		System.out.println("F2:"+A.apply(B.apply(5)));
		System.out.println("F2:" + B.andThen(A).apply(5));
		
		System.out.println(B.compose(A).compose(A).andThen(A).apply(5));
		
	}
	
}
